package com.ww.grtsmanager.controller;

import com.ww.grtsmanager.bean.Chapter;
import com.ww.grtsmanager.bean.Course;
import com.ww.grtsmanager.service.CourseService;
import com.ww.grtsmanager.vo.CourseInfo;
import com.ww.grtsmanager.vo.EduResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {
    @Autowired
    private CourseService courseService;
    @RequestMapping("/addCourse")
    public EduResult addCourse(@Valid Course course, String des, BigDecimal money){
        BigDecimal price = money.multiply(BigDecimal.valueOf(100));
        course.setPrice(price.longValue());
        int id = courseService.addCourse(course,des);
        return EduResult.ok(id);
    }
    @RequestMapping("/getChapterAll/{id}")
    public EduResult getChapterAll(@PathVariable Integer id){
        List<Chapter> list = courseService.getChapterAll(id);
        return EduResult.ok(list);
    }
    @RequestMapping("/getCourse/{id}")
    public EduResult getCourse(@PathVariable Integer id){
        CourseInfo courseInfo = courseService.getCourse(id);
        return EduResult.ok(courseInfo);
    }
    @RequestMapping("/updateCourse")
    public EduResult updateCourse(@Valid Course course, String des, BigDecimal money){
        BigDecimal price = money.multiply(BigDecimal.valueOf(100));
        course.setPrice(price.longValue());
        courseService.updateCourse(course,des);
        return EduResult.ok();
    }

}
